home *** CD-ROM | disk | FTP | other *** search
/ American Osteopathic Ass…tion Yearbook 2005 & 2006 / American Osteopathic Association Yearbook 2005 & 2006.iso / mac / app / applesingle.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2004-07-22  |  3.6 KB  |  120 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.3)
  3.  
  4. import struct
  5. import MacOS
  6. import sys
  7. Error = 'applesingle.Error'
  8. verbose = 0
  9. AS_HEADER_FORMAT = 'll16sh'
  10. AS_HEADER_LENGTH = 26
  11. AS_MAGIC = 333312
  12. AS_VERSION = 131072
  13. AS_ENTRY_FORMAT = 'lll'
  14. AS_ENTRY_LENGTH = 12
  15. AS_DATAFORK = 1
  16. AS_RESOURCEFORK = 2
  17. AS_IGNORE = (3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15)
  18.  
  19. def decode(input, output, resonly = 0):
  20.     if type(input) == type(''):
  21.         input = open(input, 'rb')
  22.     
  23.     header = input.read(AS_HEADER_LENGTH)
  24.     
  25.     try:
  26.         (magic, version, dummy, nentry) = struct.unpack(AS_HEADER_FORMAT, header)
  27.     except ValueError:
  28.         arg = None
  29.         raise Error, 'Unpack header error: %s' % arg
  30.  
  31.     if verbose:
  32.         print 'Magic:   0x%8.8x' % magic
  33.         print 'Version: 0x%8.8x' % version
  34.         print 'Entries: %d' % nentry
  35.     
  36.     if magic != AS_MAGIC:
  37.         raise Error, 'Unknown AppleSingle magic number 0x%8.8x' % magic
  38.     
  39.     if version != AS_VERSION:
  40.         raise Error, 'Unknown AppleSingle version number 0x%8.8x' % version
  41.     
  42.     if nentry <= 0:
  43.         raise Error, 'AppleSingle file contains no forks'
  44.     
  45.     headers = [ input.read(AS_ENTRY_LENGTH) for i in range(nentry) ]
  46.     didwork = 0
  47.     for hdr in headers:
  48.         
  49.         try:
  50.             (id, offset, length) = struct.unpack(AS_ENTRY_FORMAT, hdr)
  51.         except ValueError:
  52.             []
  53.             arg = []
  54.             []
  55.             raise Error, 'Unpack entry error: %s' % arg
  56.         except:
  57.             []
  58.  
  59.         if verbose:
  60.             print 'Fork %d, offset %d, length %d' % (id, offset, length)
  61.         
  62.         input.seek(offset)
  63.         if length == 0:
  64.             data = ''
  65.         else:
  66.             data = input.read(length)
  67.         if len(data) != length:
  68.             raise Error, 'Short read: expected %d bytes got %d' % (length, len(data))
  69.         
  70.         if id == AS_DATAFORK:
  71.             if verbose:
  72.                 print '  (data fork)'
  73.             
  74.             if not resonly:
  75.                 didwork = 1
  76.                 fp = open(output, 'wb')
  77.                 fp.write(data)
  78.                 fp.close()
  79.             
  80.         not resonly
  81.         if id == AS_RESOURCEFORK:
  82.             didwork = 1
  83.             if verbose:
  84.                 print '  (resource fork)'
  85.             
  86.             if resonly:
  87.                 fp = open(output, 'wb')
  88.             else:
  89.                 fp = MacOS.openrf(output, 'wb')
  90.             fp.write(data)
  91.             fp.close()
  92.             continue
  93.         if id in AS_IGNORE:
  94.             if verbose:
  95.                 print '  (ignored)'
  96.             
  97.         verbose
  98.         raise Error, 'Unknown fork type %d' % id
  99.     
  100.     if not didwork:
  101.         raise Error, 'No useful forks found'
  102.     
  103.  
  104.  
  105. def _test():
  106.     if len(sys.argv) < 3 and sys.argv[1] == '-r' and len(sys.argv) != 4:
  107.         print 'Usage: applesingle.py [-r] applesinglefile decodedfile'
  108.         sys.exit(1)
  109.     
  110.     if sys.argv[1] == '-r':
  111.         resonly = 1
  112.         del sys.argv[1]
  113.     else:
  114.         resonly = 0
  115.     decode(sys.argv[1], sys.argv[2], resonly = resonly)
  116.  
  117. if __name__ == '__main__':
  118.     _test()
  119.  
  120.